package com.webchat.client.mall.controller;


import com.webchat.client.mall.service.MallUserCartService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.SafeClick;
import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.enums.ClickEvent;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.mall.MallUserCartRequestVO;
import com.webchat.domain.vo.response.mall.MallUserCartListResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mall-service/cart")
public class MallUserCartController {

    @Autowired
    private MallUserCartService mallUserCartService;

    /**
     * 加入购物车
     *
     * @param userCartRequest
     * @return
     */
    @ValidateLogin
    @SafeClick(event = ClickEvent.MALL_ADD_CART, time = 1000, message = "频繁点击")
    @PostMapping("/add")
    public APIResponseBean<Boolean> addCart(@RequestBody MallUserCartRequestVO userCartRequest) {
        String userId = SessionHelper.getCurrentUserId();
        userCartRequest.setUserId(userId);
        boolean res = mallUserCartService.addCart(userCartRequest);
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 删除购物车
     *
     * @param skuId
     * @return
     */
    @ValidateLogin
    @PostMapping("/remove/{skuId}")
    public APIResponseBean<Boolean> deleteCart(@PathVariable Long skuId) {
        String userId = SessionHelper.getCurrentUserId();
        MallUserCartRequestVO userCartRequest = new MallUserCartRequestVO();
        userCartRequest.setUserId(userId);
        userCartRequest.setSkuId(skuId);
        boolean res = mallUserCartService.deleteCart(userCartRequest);
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 购物车列表
     *
     * @return
     */
    @ValidateLogin
    @GetMapping("/page")
    public APIPageResponseBean<MallUserCartListResponseVO> page(
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        String userId = SessionHelper.getCurrentUserId();
        return mallUserCartService.page(userId, pageNo, pageSize);
    }

    /**
     * 购物车sku数量修改
     *
     * @return
     */
    @ValidateLogin
    @PostMapping("updateCount")
    public APIResponseBean<Boolean> updateCount(@RequestBody MallUserCartRequestVO userCartRequest) {
        String userId = SessionHelper.getCurrentUserId();
        userCartRequest.setUserId(userId);
        boolean res = mallUserCartService.updateCount(userCartRequest);
        return APIResponseBeanUtil.success(res);
    }
}
